<template>
  <basic-container>
    <avue-crud
        :data="data"
        :option="option"
        ref="curd"
        :page="page"
        v-model="form"
        :permission="permissionList"
        :table-loading="loading"
        :before-open="beforeOpen"
        @row-save="save"
        @row-del="del"
        @row-update="update"
        @search-change="search"
        @refresh-change="refresh"
        @selection-change="selectChange"
        @on-load="onLoad"
        @row-dblclick="editClick"
    >

      <template slot="name" slot-scope="{row}">
        <el-tag v-if="row.parentId==0">{{ row.name }}</el-tag>
        <el-tag type="danger" v-else>{{ row.name }}</el-tag>
      </template>
      <template slot="code" slot-scope="{row}">
        <el-tag v-if="row.parentId==0">{{ row.code }}</el-tag>
        <el-tag type="danger" v-else>{{ row.code }}</el-tag>
      </template>
      <template slot="menuLeft">
        <el-button
            type="primary"
            size="small"
            icon="el-icon-download"
            v-if="permission.dict_excel"
            @click="exports"
        >导 出
        </el-button>

        <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            v-if="permission.dict_delete"
            @click="deleteAll"
        >删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>
<script>
import {mapGetters} from "vuex";
import {del, exports, list, query, save, update} from "@/api/system/dict";

export default {
  data() {
    return {
      loading: true,
      form: {},
      data: [],
      selectionList: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        menuWidth: 300,
        height: 'auto',
        calcHeight: 100,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        selection: true,
        dialogClickModal: false,
        index: true,
        enter: true,
        copyBtn: true,
        viewBtn: true,
        columnBtn: false,
        tip: false,
        column: [
          {
            label: "名称",
            prop: "name",
            slot: true,
            rules: [
              {
                required: true,
                message: "请输入名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "上级",
            prop: "parentId",
            hide: true,
            type: "tree",
            props: {
              label: "name",
              value: "id",
            },
            dicUrl: `/system/dict`,
          },
          {
            label: "标志",
            prop: "code",
            slot: true,
            rules: [
              {
                required: true,
                message: "请输入标志",
                trigger: "blur",
              },
            ],
          },
          {
            label: "字典值",
            prop: "dictValue",
          },
          {
            label: "排序",
            prop: "sort",
            hide: true,
            rules: [
              {
                required: true,
                message: "请输入排序",
                trigger: "blur",
              },
            ],
          },
          {
            label: "备注",
            prop: "remark",
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        copyBtn: this.vaildData(this.permission.dict_copy, false),
        viewBtn: this.vaildData(this.permission.dict_view, false),
        addBtn: this.vaildData(this.permission.dict_add, false),
        delBtn: this.vaildData(this.permission.dict_delete, false),
        editBtn: this.vaildData(this.permission.dict_edit, false),
      };
    },
    delIds() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id + "");
      });
      return ids.join(",") + "";
    },
  },
  methods: {
    editClick(row) {
      this.$refs.curd.rowEdit(row, row.$index);
    },
    onLoad(page, param) {
      this.loading = true;
      list(param).then((res) => {
        this.loading = false;
        this.data = res.data.data;
      });
    },
    exports() {
      let url = exports()
      const a = document.createElement('a')
      a.href = url
      a.click();
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        query(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    save(row, loading, done) {
      loading();
      save(row).then((res) => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: res.data.msg,
        });
      });
      done();
    },
    deleteAll() {
      if (this.selectionList.length <= 0) {
        this.$message.error("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            return del(this.delIds);
          })
          .then((res) => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: res.data.msg,
            });
          });
    },
    del(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            return del(row.id);
          })
          .then((res) => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: res.data.msg,
            });
          });
    },
    update(row, index, loading, done) {
      loading();
      update(row).then((res) => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: res.data.msg,
        });
      });
      done();
    },
    refresh() {
      this.onLoad(this.page, "");
    },
    search(params, done) {
      this.onLoad(this.page, params);
      done();
    },
    selectChange(list) {
      this.selectionList = list;
    },
  },
}
</script>
